home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / Sound / SndPlayDoubleBuffer / _source / ReadResource.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-15  |  5.8 KB  |  189 lines  |  [TEXT/CWIE]

  1. /*
  2. **    Apple Macintosh Developer Technical Support
  3. **
  4. **    Routines demonstrating how to read resource files without using
  5. **    the Resource Manager.
  6. **
  7. **    by Mark Cookson, Apple Developer Technical Support
  8. **
  9. **    File:    ReadResource.c
  10. **
  11. **    Copyright ©1996 Apple Computer, Inc.
  12. **    All rights reserved.
  13. **
  14. **    You may incorporate this sample code into your applications without
  15. **    restriction, though the sample code has been provided "AS IS" and the
  16. **    responsibility for its operation is 100% yours.  However, what you are
  17. **    not permitted to do is to redistribute the source as "Apple Sample
  18. **    Code" after having made changes. If you're going to re-distribute the
  19. **    source, we require that you make it clear in the source that the code
  20. **    was descended from Apple Sample Code, but that you've made changes.
  21. */
  22.  
  23. #include "ReadResource.h"
  24.  
  25. /*    Purpose:        This finds the number of the first resource of chosen
  26.                     type and returns that number, along with any error.
  27.     Side Effects:    None.
  28. */
  29. /*-----------------------------------------------------------------------*/
  30.         OSErr    MyGetFirstResource        (short refNum,
  31.                                         OSType targetType,
  32.                                         short *targetID)
  33. /*-----------------------------------------------------------------------*/
  34. {
  35.     ResReference    theRes;
  36.     long            dataOffset        = 0,
  37.                     typeOffset        = 0,
  38.                     ResRefSize        = 0,
  39.                     oldPos            = 0;
  40.     short            numResources    = 0;
  41.     OSErr            theErr            = noErr;
  42.  
  43.     theErr = GetFPos (refNum, &oldPos);        /* Be nice and don't move the file pointer without puting it back */
  44.     if (theErr == noErr) {
  45.         /* Find the location of the first resource of the type we want */
  46.         theErr = MyGetTypesPosition (refNum, targetType, &numResources, &dataOffset, &typeOffset);
  47.     }
  48.     if (theErr == noErr) {
  49.         /* Position the file to the start of the list of resources */
  50.         theErr = SetFPos (refNum, fsFromStart, typeOffset);
  51.     }
  52.     if (theErr == noErr) {
  53.         ResRefSize = sizeof (theRes);
  54.         if (numResources != 0) {
  55.             theErr = FSRead (refNum, &ResRefSize, &theRes);
  56.             *targetID = theRes.ID;
  57.             if (theErr == noErr) {
  58.                 theErr = SetFPos (refNum, fsFromStart, oldPos);
  59.             }
  60.         }
  61.         else {
  62.             theErr = resNotFound;
  63.         }
  64.     }
  65.  
  66.     return theErr;
  67. }
  68.  
  69. /*    Purpose:        This finds the requested resource and positions the file
  70.                     pointer to point to the first byte of that resource.  It
  71.                     returns the position, inside the resource fork, of that
  72.                     resource, or if unsuccessful the error.
  73.     Side Effects:    None.
  74. */
  75. /*-----------------------------------------------------------------------*/
  76.         OSErr    MyGetResourcePosition    (short refNum,
  77.                                         OSType targetType,
  78.                                         short targetID,
  79.                                         long *firstByte)
  80. /*-----------------------------------------------------------------------*/
  81. {
  82.     ResReference    theRes;
  83.     long            position        = 0,
  84.                     dataOffset        = 0,
  85.                     typeOffset        = 0,
  86.                     ResRefSize        = 0;
  87.     short            numResources    = 0;
  88.     OSErr            theErr            = noErr;
  89.     Boolean            found            = false;
  90.  
  91.     /* Find the location of the first resource of the type we want */
  92.     theErr = MyGetTypesPosition (refNum, targetType, &numResources, &dataOffset, &typeOffset);
  93.     if (theErr == noErr) {
  94.         /* Position the file to the start of the list of resources */
  95.         theErr = SetFPos (refNum, fsFromStart, typeOffset);
  96.     }
  97.     if (theErr == noErr) {
  98.         ResRefSize = sizeof (theRes);
  99.     }
  100.     while (theErr == noErr && numResources-- && found == false) {
  101.         theErr = FSRead (refNum, &ResRefSize, &theRes);
  102.         if (theErr == noErr && theRes.ID == targetID) {
  103.             position = (theRes.dataOffset & kDataOffset) + dataOffset;
  104.             *firstByte = position + sizeof (long);
  105.             found = true;
  106.         }
  107.     }
  108.     if (theErr == noErr) {
  109.         theErr = SetFPos (refNum, fsFromStart, *firstByte);
  110.     }
  111.  
  112.     if (found == false) {
  113.         theErr = resNotFound;
  114.     }
  115.  
  116.     return theErr;
  117. }
  118.  
  119. /*    Purpose:        This finds the beginning of the list of requested
  120.                     resource types (i.e. all 'snd ' resources).  It returns
  121.                     the offset to these resources, or if unsuccessful the
  122.                     error.
  123.     Side Effects:    None.
  124. */
  125. /*-----------------------------------------------------------------------*/
  126.         OSErr    MyGetTypesPosition    (short refNum,
  127.                                     OSType targetType,
  128.                                     short *numResources,
  129.                                     long *dataOffset,
  130.                                     long *firstByteOfTypeList)
  131. /*-----------------------------------------------------------------------*/
  132. {
  133.     ResourceHeader            ResForkHeader;
  134.     ResourceMap                ResMap;
  135.     ResourceTypeListEntry    typeListEntry;
  136.     long                    numTypes        = 0,
  137.                             numBytes        = 0,
  138.                             numBytesToRead    = 0,
  139.                             position        = 0,
  140.                             oldPos            = 0;
  141.     OSErr                    theErr            = noErr;
  142.     Boolean                    found            = false;
  143.  
  144.     theErr = GetFPos (refNum, &oldPos);        /* Be nice and don't move the file pointer without puting it back */
  145.     if (theErr == noErr) {
  146.         theErr = SetFPos (refNum, fsFromStart, 0);
  147.     }
  148.     if (theErr == noErr) {
  149.         /* Read the resource file header */
  150.         numBytesToRead = sizeof (ResForkHeader);
  151.         theErr = FSRead (refNum, &numBytesToRead, &ResForkHeader);
  152.     }
  153.     if (theErr == noErr) {
  154.         /* Read the resource map */
  155.         theErr = SetFPos (refNum, fsFromStart, ResForkHeader.resMapOffset);
  156.     }
  157.     if (theErr == noErr) {
  158.         numBytesToRead = sizeof (ResMap);
  159.         theErr = FSRead (refNum, &numBytesToRead, &ResMap);
  160.     }
  161.     if (theErr == noErr) {
  162.         /* Find the type list */
  163.         position = ResMap.typesListOffset + ResForkHeader.resMapOffset + 2;
  164.         theErr = SetFPos (refNum, fsFromStart, position);
  165.     }
  166.     if (theErr == noErr) {
  167.         numTypes = ResMap.numTypesInMap + 1;
  168.         position += sizeof (numTypes);
  169.         numBytes = sizeof (typeListEntry);
  170.     }
  171.     while (theErr == noErr && numTypes-- && found == false) {
  172.         theErr = FSRead (refNum, &numBytes, &typeListEntry);
  173.         if (theErr == noErr && typeListEntry.resType == targetType) {
  174.             *firstByteOfTypeList = ResForkHeader.resMapOffset + ResMap.typesListOffset + typeListEntry.referenceOffset;
  175.             *numResources = typeListEntry.numEntries + 1;
  176.             *dataOffset = ResForkHeader.resDataOffset;
  177.             found = true;
  178.         }
  179.     }
  180.  
  181.     theErr = SetFPos (refNum, fsFromStart, oldPos);
  182.  
  183.     if (found == false) {
  184.         theErr = resNotFound;
  185.     }
  186.  
  187.     return theErr;
  188. }
  189.